package com.lzp.aop.strategy;

import com.lzp.aop.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author : lzp
 * @version 1.0
 * @date : 2022/8/30 11:05
 * @apiNote : 策略获取上下文
 */
@Component
public class StrategyContext {
    private final static ConcurrentHashMap<String, TestService> TEST_SERVICE_CONCURRENT_HASH_MAP=new ConcurrentHashMap<>(16);
    @Autowired
    private void putStrategy(Map<String,TestService> serviceMap){
        //这里的key不是对应类型接口的实现的首字母小写string字符串,而是注册在spring里的bean名
        //@Service("ThreeExtendTestServiceImpl") 如果没有设置,会是threeExtendTestServiceImpl,设置后会是ThreeExtendTestServiceImpl
        serviceMap.forEach((s, testService) -> System.out.println("key:"+s+"  value:"+testService.getClass().getSimpleName()));
        TEST_SERVICE_CONCURRENT_HASH_MAP.putAll(serviceMap);
    }

    public static TestService getTestServiceConcurrentHashMap(Integer type) {
        String nameByType = ServiceEnum.getNameByType(type);
        //if (!StringUtils.isEmpty(nameByType)) {
        //    String[] split = nameByType.split("");
        //    if (split.length>0) {
        //        split[0]=split[0].toLowerCase();
        //        nameByType = String.join("", split);
        //    }
        //}
        return TEST_SERVICE_CONCURRENT_HASH_MAP.get(nameByType);
    }
}
